package com.skyworth.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yuhaichao on 2016/9/27.
 */
public class DataConvert {

    public static Map<String,Object> objectConvertMap(Object obj, String[] keyArr){
        try {
            if(obj==null || keyArr==null || keyArr.length<=0){
                return null;
            }
            Object[] objArr = (Object[]) obj;
            if(objArr==null || objArr.length<=0){
                return null;
            }
            Map<String, Object> map = new HashMap<>();
            for (int i = 0; i < objArr.length; i++) {
                map.put(keyArr[i], objArr[i]);
            }
            return map;
        }catch(Exception e){
            return null;
        }
    }

    public static List<Map<String,Object>> objectListConvertMapList(List<Object> objList, String[] keyArr){
        try {
            if(objList==null || objList.size()<=0 || keyArr==null || keyArr.length<=0){
                return null;
            }
            List<Map<String,Object>> list = new ArrayList<>();
            for(int i=0;i<objList.size();i++){
                Map<String, Object> map = new HashMap<>();
                Object[] objArr = (Object[]) objList.get(i);
                for (int j = 0; j < objArr.length; j++) {
                    map.put(keyArr[j], objArr[j]);
                }
                list.add(map);
            }

            return list;
        }catch(Exception e){
            return null;
        }
    }

}
